
    /*********************************************************
     *
     *  Project:      HZFlashcards
     *
     *  Class:        HanZiCounterPanel
     *
     *  File name:    HanZiCounterPanel.java
     *
     *  Sep 5, 2007
     *
     *********************************************************/

package com.eastmountain.chinese.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import com.eastmountain.chinese.common.LessonStatus;
import com.eastmountain.chinese.observable.HZFlashcardsDataModel;

@SuppressWarnings("serial")
public class HanZiCounterPanel extends JPanel
                               implements Observer {

    private HZFlashcardsDataModel hzFlashcardsDataModel;

    private GridBagConstraints constraints = new GridBagConstraints();

    private JLabel title;

    private JLabel levelLabel;
    private JLabel level;

    private JLabel lessonNumberLabel;
    private JLabel lessonNumber;

    private JLabel cardNumberLabel;
    private JLabel cardNumber;

    private JLabel cardsInSetLabel;
    private JLabel cardsInSet;

    private static final int LABEL_HEIGHT = 45;
    private static final int GAP = 2;

    private static final Insets INSETS = new Insets( GAP, GAP, GAP, GAP );

    private Border emptyBorder = BorderFactory.createEmptyBorder( HanZiCounterPanel.GAP, HanZiCounterPanel.GAP, HanZiCounterPanel.GAP, HanZiCounterPanel.GAP );
    private Border linedBorder = BorderFactory.createLineBorder( Color.LIGHT_GRAY, 1 );

    private Border etchedBorder = BorderFactory
                    .createBevelBorder( BevelBorder.LOWERED );
    private Border compoundBorder = BorderFactory.createCompoundBorder( emptyBorder, etchedBorder );

    public HanZiCounterPanel() {

        hzFlashcardsDataModel = HZFlashcardsDataModel.getInstance();
        hzFlashcardsDataModel.addObserver( this );

        setLayout( new GridBagLayout() );
        setBorder( BorderFactory.createLineBorder( Color.GRAY, 2 ) );

        title = new JLabel( "" );
        title.setHorizontalAlignment( SwingConstants.CENTER );
        title.setPreferredSize( new Dimension( 200, HanZiCounterPanel.LABEL_HEIGHT ) );
        title.setBorder( linedBorder );

        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 7;
        constraints.gridheight = 1;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.insets = INSETS;

        title.setText( "This is positively a lesson" );

        add( title, constraints );

        createStatusFields();

    }

    private void createStatusFields() {

       levelLabel = new JLabel( "Level" );
       levelLabel.setBorder( emptyBorder );

       constraints.gridx = 0;
       constraints.gridy = 1;
       constraints.gridwidth = 1;
       constraints.gridheight = 1;
       constraints.anchor = GridBagConstraints.WEST;
       constraints.fill = GridBagConstraints.NONE;
       constraints.weightx = 0;
       constraints.weighty = 0;
       constraints.ipadx = 4;
       constraints.ipady = 4;
       constraints.insets = INSETS;

       add( levelLabel, constraints );

       level = new JLabel( "" );
       level.setHorizontalAlignment( SwingConstants.CENTER );
       level.setPreferredSize( new Dimension( 35, HanZiCounterPanel.LABEL_HEIGHT ) );
       level.setBorder( compoundBorder );

       constraints.gridx = 1;
       constraints.gridy = 1;
       constraints.gridwidth = 1;
       constraints.gridheight = 1;

       add( level, constraints );

       lessonNumberLabel = new JLabel( "Lesson" );
       lessonNumberLabel.setBorder( emptyBorder );

       constraints.gridx = 2;
       constraints.gridy = 1;
       constraints.gridwidth = 1;
       constraints.gridheight = 1;

       add( lessonNumberLabel, constraints );

       lessonNumber = new JLabel( "" );
       lessonNumber.setHorizontalAlignment( SwingConstants.RIGHT );
       lessonNumber.setPreferredSize( new Dimension( 45, HanZiCounterPanel.LABEL_HEIGHT ) );
       lessonNumber.setBorder( compoundBorder );

       constraints.gridx = 3;
       constraints.gridy = 1;
       constraints.gridwidth = 1;
       constraints.gridheight = 1;

       add( lessonNumber, constraints );

       cardNumberLabel = new JLabel( "Card" );
       cardNumberLabel.setBorder( emptyBorder );

       constraints.gridx = 4;
       constraints.gridy = 1;
       constraints.gridwidth = 1;
       constraints.gridheight = 1;

       add( cardNumberLabel, constraints );

       cardNumber = new JLabel( "" );
       cardNumber.setHorizontalAlignment( SwingConstants.RIGHT );
       cardNumber.setPreferredSize( new Dimension( 45, HanZiCounterPanel.LABEL_HEIGHT ) );
       cardNumber.setBorder( compoundBorder );

       constraints.gridx = 5;
       constraints.gridy = 1;
       constraints.gridwidth = 1;
       constraints.gridheight = 1;

       add( cardNumber, constraints );

       cardsInSetLabel = new JLabel( "Total Cards" );
       cardsInSetLabel.setBorder( emptyBorder );

       constraints.gridx = 6;
       constraints.gridy = 1;
       constraints.gridwidth = 1;
       constraints.gridheight = 1;

       add( cardsInSetLabel, constraints );

       cardsInSet = new JLabel( "" );
       cardsInSet.setHorizontalAlignment( SwingConstants.RIGHT );
       cardsInSet.setPreferredSize( new Dimension( 45, HanZiCounterPanel.LABEL_HEIGHT ) );
       cardsInSet.setBorder( compoundBorder );

       constraints.gridx = 7;
       constraints.gridy = 1;
       constraints.gridwidth = 1;
       constraints.gridheight = 1;

       add( cardsInSet, constraints );

   }

    public String getTitle() {

        return ( title.getText() );

    }

    public void setTitle( String title ) {

        this.title.setText( title );

    }

    public String getLevel() {

        return level.getText();

    }

    public void setLevel( String level ) {

        this.level.setText( level );

    }

    public String getLessonNumber() {

        return lessonNumber.getText();

    }

    public void setLessonNumber( int lessonNo ) {

        lessonNumber.setText( Integer.toString( lessonNo ) );

    }
    public String getCardNumber() {

        return cardNumber.getText();

    }

    public void setCardNumber( int cardNo ) {

        cardNumber.setText( Integer.toString( cardNo ) );

    }

    public String getCardsInSet() {
        
        return cardsInSet.getText();

    }

    public void setCardsInSet( int cardsInFCSet ) {

        cardsInSet.setText( Integer.toString( cardsInFCSet ) );

    }

    /***********************************************************************************************
     *
     * update
     *
     **********************************************************************************************/

    public void update( Observable o, Object arg ) {

        System.out.println( "!!! HanZiCounterPanel.update() invoked  !!!!" );
        System.out.println( "Class name: " + arg.getClass().getSimpleName() );

        if ( arg == null  ) {

            System.out.println( "   --- arg is null ---" );

            return;

        }

        if ( "LessonStatus".equals( arg.getClass().getSimpleName() ) ) {

            LessonStatus lessonStatus = (LessonStatus) arg;

            System.out.println( "Got a " + arg.getClass().getSimpleName() );

            setTitle( lessonStatus.getTitle() );
            setLevel( lessonStatus.getLevel() );
            setLessonNumber( lessonStatus.getLessonNumber() );
            setCardNumber( lessonStatus.getCardNumberInLesson() );
            setCardsInSet( lessonStatus.getNumberOfCardsInSet() );


            System.out.println( "LessonStatus: " + lessonStatus );

            //repaint();

        }

    }


}
